﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using NormReference.Models;
using System.Data;
using System.Data.OleDb;
using NormReference.Services;
using Excel;
using System.IO;

namespace NormReference.DAL
{
    public class NormReferenceInitializer : DropCreateDatabaseIfModelChanges<NormReferenceContext>
    {
        protected override void Seed(NormReferenceContext context)
        {

            Logger.getInstance().printToLogFile("Database initialize!");
            try
            {
                FileStream stream = File.Open("C:\\Documents and Settings\\Kyle Hawkins\\Desktop\\CAPSTONE-FINAL\\NormReferenceFiles\\IMPORT.xls", FileMode.Open, FileAccess.Read);
                IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
                excelReader.IsFirstRowAsColumnNames = true;
                DataSet result = excelReader.AsDataSet();
                DataTable table = result.Tables[0];
                excelReader.Close();

                var courseevaluations = new List<CourseEvaluation>();
                foreach (DataRow row in table.Rows)
                {
                    CourseEvaluation evaluation = new CourseEvaluation();
                    evaluation.surveyname = row["surveyname"].ToString();
                    evaluation.dateStart = row["datestart"].ToString();
                    evaluation.dateClose = row["dateclose"].ToString();
                    evaluation.crsnum = row["crsnum"].ToString();
                    evaluation.crsname = row["crsname"].ToString();
                    evaluation.department = row["department"].ToString();
                    evaluation.division = row["division"].ToString();
                    evaluation.crs_dir = row["crs_dir"].ToString();
                    evaluation.resp_fac = row["resp_fac"].ToString();
                    evaluation.gender = row["gender"].ToString();
                    evaluation.core1 = row["core1"].ToString().Trim();
                    evaluation.core2 = row["core2"].ToString().Trim();
                    evaluation.core3 = row["core3"].ToString().Trim();
                    evaluation.core4 = row["core4"].ToString().Trim();
                    evaluation.core5 = row["core5"].ToString().Trim();
                    evaluation.core6 = row["core6"].ToString().Trim();
                    evaluation.core7 = row["core7"].ToString().Trim();
                    evaluation.core8 = row["core8"].ToString().Trim();
                    evaluation.core9 = row["core9"].ToString().Trim();
                    evaluation.core10 = row["core10"].ToString().Trim();
                    evaluation.core11 = row["core11"].ToString();
                    evaluation.core12 = row["core12"].ToString();
                    evaluation.core13 = row["core13"].ToString();
                    evaluation.core14 = row["core14"].ToString().Trim();
                    evaluation.core15 = row["core15"].ToString().Trim();
                    evaluation.core16 = row["core16"].ToString().Trim();
                    evaluation.core17 = row["core17"].ToString().Trim();
                    evaluation.core18 = row["core18"].ToString().Trim();
                    evaluation.core19 = row["core19"].ToString().Trim();
                    evaluation.core20 = row["core20"].ToString().Trim();
                    evaluation.core21 = row["core21"].ToString().Trim();
                    evaluation.core22 = row["core22"].ToString().Trim();
                    evaluation.core23 = row["core23"].ToString().Trim();
                    evaluation.core24 = row["core24"].ToString().Trim();
                    evaluation.core25 = row["core25"].ToString().Trim();
                    evaluation.core26 = row["core26"].ToString().Trim();
                    evaluation.core27 = row["core27"].ToString().Trim();
                    evaluation.core28 = row["core28"].ToString().Trim();
                    evaluation.core29 = row["core29"].ToString();
                    evaluation.core30 = row["core30"].ToString();
                    courseevaluations.Add(evaluation);
                }

                courseevaluations.ForEach(s => context.courseEvaluations.Add(s));
                context.SaveChanges();


            }
            catch (Exception e)
            {
                Logger.getInstance().printToLogFile(e.ToString());
            }
            
            var divisions = new List<Division> 
            {
                new Division{code = "PGS", name = "Professional and Graduate Studies"},
                new Division{code = "NHS", name = "Natural and Health Sciences"},
                new Division{code = "HSS", name = "Humanities and Social Sciences"}
            };
            divisions.ForEach(s => context.divisions.Add(s));
            context.SaveChanges();

            var comparisonGroupQueue_Backup = new List<ComparisonGroupQueue_Backup>
            {
                new ComparisonGroupQueue_Backup()
            };
            comparisonGroupQueue_Backup.ForEach(s => context.comparisonGroupQueue_Backup.Add(s));
            context.SaveChanges();

            var faculty = new List<Faculty>
            {
                new Faculty{name = "Cynthia Horst", email = "khawkins@carrollu.edu"},
                new Faculty{name = "Steven Staab", email = "khawkins@carrollu.edu"},
                new Faculty{name = "Chenglie Hu", email = "khawkins@carrollu.edu"},
            };
            faculty.ForEach(s => context.faculty.Add(s));
            context.SaveChanges();
        }
    }
}